package com.ihomey.linkuphome.ui;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ihomey.linkuphome.DeviceController;
import com.ihomey.linkuphome.R;
import com.ihomey.linkuphome.base.BaseFragment;
import com.ihomey.linkuphome.controller.Controller;
import com.ihomey.linkuphome.controller.ControllerFactory;
import com.ihomey.linkuphome.entities.Device;
import com.ihomey.linkuphome.entities.LightState;
import com.ihomey.linkuphome.util.BindView;
import com.ihomey.linkuphome.widget.LoopView;
import com.suke.widget.SwitchButton;

import java.util.ArrayList;
import java.util.Calendar;


/**
 * Created by Administrator on 2017/4/20.
 */

public class TimerSettingFragment extends BaseFragment implements View.OnClickListener {

    @BindView(id = R.id.frameLayout_timer_setting)
    private RelativeLayout frameLayout_timer_setting;
    @BindView(id = R.id.rg_timer_setting)
    private RadioGroup rg_timer_setting;
    @BindView(id = R.id.tv_timer_setting_type)
    private TextView tv_timer_setting_type;
    @BindView(id = R.id.switch_button_timer)
    private SwitchButton switch_button_timer;

    @BindView(id = R.id.tv_timer_setting_hour_edit)
    private LoopView tv_timer_setting_hour_edit;
    @BindView(id = R.id.tv_timer_setting_minute_edit)
    private LoopView tv_timer_setting_minute_edit;

    private DeviceController mController;
    private Controller controller;
    private LightState lightState;

    public static TimerSettingFragment newInstance() {
        TimerSettingFragment fragment = new TimerSettingFragment();
        return fragment;
    }



    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        View view = inflater.inflate(R.layout.fragment_timer_setting, null);
        return view;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mController = (DeviceController) activity;
            controller = new ControllerFactory().createController(mController.getDeviceType());
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement DeviceController callback interface.");
        }
    }


    public ArrayList<String> getMinuteList() {
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < 60; i++) {
            if (i < 10) {
                list.add("0" + i);
            } else {
                list.add("" + i);
            }
        }
        return list;
    }

    public ArrayList<String> getHourList() {
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < 24; i++) {
            if (i < 10) {
                list.add("0" + i);
            } else {
                list.add("" + i);
            }

        }
        return list;
    }

    @Override
    public void initWidget(View parentView) {
        super.initWidget(parentView);
        MainActivity mainActivity = (MainActivity) getActivity();
        mainActivity.setTitle(R.string.timer);
        mainActivity.setRightText(R.string.editing);
        mainActivity.getRightTv().setTag(true);
        mainActivity.getRightTv().setOnClickListener(this);

        tv_timer_setting_hour_edit.setLabel("h");
        tv_timer_setting_hour_edit.setTextSize(48);
        tv_timer_setting_hour_edit.setDataList(getHourList());

        tv_timer_setting_minute_edit.setLabel("m");
        tv_timer_setting_minute_edit.setTextSize(48);
        tv_timer_setting_minute_edit.setDataList(getMinuteList());

        rg_timer_setting.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                int radioButtonId = radioGroup.getCheckedRadioButtonId();
                lightState = mController.getLightState(mController.getSelectedDeviceId());
                if (radioButtonId == R.id.rb_timer_setting_on) {
                    tv_timer_setting_type.setText(R.string.lightOpenTime);
                    tv_timer_setting_hour_edit.setCircleColor(1);
                    tv_timer_setting_minute_edit.setCircleColor(1);
                    frameLayout_timer_setting.setBackgroundResource(R.drawable.timer_setting_on_bg);
                    if (lightState != null) {
                        tv_timer_setting_hour_edit.setInitPosition(lightState.getOpenTimer() / 60);
                        tv_timer_setting_minute_edit.setInitPosition(lightState.getOpenTimer() % 60);
                    }
                } else {
                    tv_timer_setting_type.setText(R.string.lightCloseTime);
                    tv_timer_setting_hour_edit.setCircleColor(0);
                    tv_timer_setting_minute_edit.setCircleColor(0);
                    frameLayout_timer_setting.setBackgroundResource(R.drawable.timer_setting_off_bg);
                    if (lightState != null) {
                        tv_timer_setting_hour_edit.setInitPosition(lightState.getCloseTimer() / 60);
                        tv_timer_setting_minute_edit.setInitPosition(lightState.getCloseTimer() % 60);
                    }
                }
            }
        });

        switch_button_timer.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton view, boolean isChecked) {
                if (rg_timer_setting.getCheckedRadioButtonId() == R.id.rb_timer_setting_on && lightState != null) {
                    if (isChecked) {
                        lightState.setIsOnOpenTimer(1);
                        if (mController.getSelectedDeviceId() != Device.DEVICE_ID_UNKNOWN) {
                            controller.setTimer(mController.getSelectedDeviceId(),getPeriodMinute(lightState.getOpenTimer()/60,lightState.getOpenTimer()%60), true);
                        }
                    } else {
                        lightState.setIsOnOpenTimer(0);
                    }
                } else if (lightState != null) {
                    if (isChecked) {
                        lightState.setIsOnCloseTimer(1);
                        if (mController.getSelectedDeviceId() != Device.DEVICE_ID_UNKNOWN) {
                            controller.setTimer(mController.getSelectedDeviceId(),getPeriodMinute(lightState.getCloseTimer()/60,lightState.getCloseTimer()%60), false);
                        }
                    } else {
                        lightState.setIsOnCloseTimer(0);
                    }
                }
            }
        });
    }


    @Override
    public void onClick(View view) {
        MainActivity mainActivity = (MainActivity) getActivity();
        if (view.getId() == R.id.toolbar_right) {
            if (view.getTag() != null && (boolean) view.getTag()) {
                tv_timer_setting_hour_edit.setCanEdit(true);
                tv_timer_setting_minute_edit.setCanEdit(true);
                view.setTag(null);
                mainActivity.setRightText(R.string.save);
            } else {
                if (lightState != null) {
                    if (rg_timer_setting.getCheckedRadioButtonId() == R.id.rb_timer_setting_on) {
                        lightState.setOpenTimer(tv_timer_setting_hour_edit.getSelectedItem() * 60 + tv_timer_setting_minute_edit.getSelectedItem());
                    } else {
                        lightState.setCloseTimer(tv_timer_setting_hour_edit.getSelectedItem() * 60 + tv_timer_setting_minute_edit.getSelectedItem());
                    }
                    mController.updateDeviceLightState(lightState);
                }
                mainActivity.setRightText(R.string.editing);
                tv_timer_setting_hour_edit.setCanEdit(false);
                tv_timer_setting_minute_edit.setCanEdit(false);
                view.setTag(true);
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        lightState = mController.getLightState(mController.getSelectedDeviceId());
        if (lightState != null) {
            tv_timer_setting_hour_edit.setInitPosition(lightState.getOpenTimer() / 60);
            tv_timer_setting_minute_edit.setInitPosition(lightState.getOpenTimer() % 60);
        }
    }

    private int getPeriodMinute(int selectHour, int selectMinute) {
        Calendar calendar = Calendar.getInstance();
        int currentHour = calendar.get(Calendar.HOUR_OF_DAY);
        int currentMinute = calendar.get(Calendar.MINUTE);
        if (selectHour > currentHour || (selectHour == currentHour && selectMinute >= currentMinute)) {
            return (selectHour - currentHour) * 60 + selectMinute - currentMinute;
        } else {
            return (selectHour - currentHour + 24) * 60 + selectMinute - currentMinute;
        }
    }
}
